<?php

namespace app\api\controller;

use app\api\model\Order;
use app\api\model\Time;
use app\common\model\Config;
use think\Db;

/**
 * 定时任务
 */
class Timingtask
{
    protected $noNeedLogin = ['cancelOrder','statusOrder'];
    protected $noNeedRight = '*';

    /**
     *  定时运行
     */
    public function run(){
        $this->cancelOrder();
        $this->statusOrder();
    }


    public function times(){
        $list = (new Time())->where(['venue_id' => 1])->select()->toArray();
        foreach ($list as $key=>$item){
            $item['venue_id'] = 3;
            unset($item['id']);
            $data[] = $item;
        }

        (new Time())->insertAll($data);
    }


    /**
     *  定时取消订单
     */
    public function cancelOrder(){
        $cancel_time = Config::field('id,createtime,status')->where('name', 'cancel_time')->value('value');
        $time = bcsub(time(), $cancel_time*60, 0);
        $user = Order::where(['is_delete' => 1, 'status' => 1])->where('createtime', '<', $time)->chunk(20, function($orders) {
            $order_list = $orders->toArray();
            $order_id_list = array_column($order_list, 'id');
            Order::whereIn('id', $order_id_list)->update(['status' => 4, 'updatetime' => time(), 'cancel_time' => time()]);
        });
    }

    /**
     *  状态修改
     */
    public function statusOrder(){
        $user = Order::field('id,createtime,status,time_list,date')->where(['is_delete' => 1, 'status' => 2])->chunk(20, function($orders) {
            if($orders){

                $start_order_ids = [];
                $end_order_ids = [];
                $order_list = $orders->toArray();
                foreach($order_list as $key => $value){
                    $time_list = json_decode($value['time_list'], true);
                    $time_list = array_column($time_list, 'time');
                    $timeData = [];
                    foreach ($time_list as $kk => $vv){
                        if($vv == '全天畅打'){
                            $timeData[] = strtotime($value['date'] . ' 00:00:00');
                            $timeData[] = strtotime($value['date'] . ' 23:59:59');
                        }else{
                            $time = explode('-', $vv);
                            $timeData[] = strtotime($value['date'] . ' ' .$time[0]);
                            $timeData[] = strtotime($value['date'] . ' ' .$time[1]);
                        }
                    }

                    $time_min = min($timeData);
                    $time_max = max($timeData);
                    if($time_min < time()){
                        $start_order_ids[] = $value['id'];
                    }
                    if($time_max < time()){
                        $end_order_ids[] = $value['id'];
                    }
                }
                if($start_order_ids){
                    Order::whereIn('id', $start_order_ids)->update(['use_status' => 2, 'updatetime' => time()]);
                }
                if($end_order_ids){
                    Order::whereIn('id', $end_order_ids)->update(['use_status' => 3, 'status' => 3, 'updatetime' => time()]);
                }
            }
        });
    }

}
